package ua.kiev.tosik.price.modules.clients.gui.files.upload;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXStatusBar;
import org.jdesktop.swingx.plaf.basic.BasicStatusBarUI;
import ua.kiev.tosik.price.models.Clients;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FilesDialog extends JDialog {

	private Clients clients;

	private JPanel panel;
	private ModuleConfig config;
	private ActionListener closeAction;
	private boolean lockDialog = false;
	private JDialog owner;

	private JLabel fileNameLabel;
	private JLabel fileName;
	private JLabel fileSizeLabel;
	private JLabel fileSize;
	private JLabel fileDateLabel;
	private JXDatePicker datePicker;

	private JButton saveButton;
	private JButton closeButton;

	private JProgressBar progressBar;

	private JXStatusBar statusBar;
	private JXStatusBar.Constraint c1;
	private JLabel imageLabel;
	private JLabel statusLabel;

	private NumberFormat formatter;

	private File file;

	public FilesDialog(JDialog owner) {
		super(owner);
		this.owner = owner;
		config = ModuleConfig.getInstance();
		formatter = new DecimalFormat("#,###");
		closeAction = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				close();
			}
		};
		panel = new JPanel( new BorderLayout() );
		statusBar = new JXStatusBar();
		c1 = new JXStatusBar.Constraint();
		imageLabel = new JLabel();
		statusLabel = new JLabel();
		statusBar.putClientProperty(BasicStatusBarUI.AUTO_ADD_SEPARATOR, true);
		c1.setFixedWidth(20);
		statusBar.add(imageLabel, c1);
		statusBar.add(statusLabel, JXStatusBar.Constraint.ResizeBehavior.FILL);
		imageLabel.setIcon(new ImageIcon(config.get("clients.editor.prices.files.uploadDialog.status.icon")));
		statusLabel.setText(config.get("clients.editor.prices.files.uploadDialog.status.text"));

		progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
		progressBar.setPreferredSize( new Dimension(80, 16) );
		progressBar.setValue(0);
		progressBar.setStringPainted(true);
		progressBar.setVisible(false);
		statusBar.add(progressBar, new JXStatusBar.Constraint(200));

		setModal(true);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		getRootPane().registerKeyboardAction(closeAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
		setSize( new Dimension(320, 240) );
		setResizable(false);
		setTitle(config.get("clients.editor.prices.files.uploadDialog.title"));
		setIconImage( new ImageIcon(config.get("clients.editor.prices.files.uploadDialog.title.icon")).getImage() );
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent ev) {
				close();
			}
		});

		fileName = new JLabel();
		fileSize = new JLabel();
		fileDateLabel = new JLabel(config.get("clients.editor.prices.files.uploadDialog.fileDate"));
		fileNameLabel = new JLabel(config.get("clients.editor.prices.files.uploadDialog.fileName"));
		fileSizeLabel = new JLabel(config.get("clients.editor.prices.files.uploadDialog.fileSize"));
		saveButton = new JButton();
		closeButton = new JButton();
		saveButton.setText(config.get("clients.editor.button.save.text"));
		saveButton.setIcon( new ImageIcon(config.get("clients.editor.button.save.icon")) );
		closeButton.setText(config.get("clients.editor.button.close.text"));
		closeButton.setIcon( new ImageIcon(config.get("clients.editor.button.close.icon")) );
		closeButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});

		datePicker = new JXDatePicker();
		datePicker.setFormats(new SimpleDateFormat("dd.MM.yyyy"));
		datePicker.setDate( new Date() );

		add(createPanel(), BorderLayout.CENTER);
		add(statusBar, BorderLayout.SOUTH);
		pack();
	}

	public Date getFileDate() {
		return datePicker.getDate();
	}

	public void setClients(Clients clients) {
		this.clients = clients;
	}

	public void setUploadAction(ActionListener saveButtonAction) {
		for ( ActionListener al: saveButton.getActionListeners() ) {
			saveButton.removeActionListener(al);
		}
		saveButton.addActionListener(saveButtonAction);
	}

	private JPanel createPanel() {
		FormLayout layout = new FormLayout(
			"right:pref, 10px, left:pref, 10px",
			"pref, 10px, pref, 10px, pref, 10px, pref, 10px, pref, 10px, pref, 10px, center:pref"
		);

		PanelBuilder builder = new PanelBuilder(layout);
		builder.setDefaultDialogBorder();
		CellConstraints cc = new CellConstraints();

		JLabel label = new JLabel( new ImageIcon(config.get("clients.editor.prices.files.uploadDialog.header.icon")));
		label.setFont(new Font(label.getFont().getFontName(), Font.BOLD, 14));
		label.setText(config.get("clients.editor.prices.files.uploadDialog.header.title"));
		label.setHorizontalAlignment(SwingConstants.LEFT);
		builder.add(label, cc.xyw(1, 1, 4));
		builder.addSeparator("", cc.xyw(1, 3, 4));
		
		builder.add(fileNameLabel, cc.xy(1, 5));
		builder.add(fileName, cc.xy(3, 5));

		builder.add(fileSizeLabel, cc.xy(1, 7));
		builder.add(fileSize, cc.xy(3, 7));

		builder.add(fileDateLabel, cc.xy(1, 9));
		builder.add(datePicker, cc.xy(3, 9));

		builder.addSeparator("", cc.xyw(1, 11, 4));

		builder.add(createButtonBar(), cc.xyw(1, 13, 4));

		return builder.getPanel();
	}


	public void open() {
		setLocationRelativeTo(owner);
		pack();
		setVisible(true);
	}

	public void close() {
		if ( !lockDialog ) {
			dispose();
			setVisible(false);
		}
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
		fileName.setText( file.getName() );
		fileSize.setText(formatter.format(file.length()/1024) + " Kb");
	}

	public void enableForm(boolean enable) {
		saveButton.setEnabled(enable);
		closeButton.setEnabled(enable);
	}

	public void setLockDialog(boolean lockDialog) {
		this.lockDialog = lockDialog;
	}

	private JPanel createButtonBar() {
		JPanel panel = new JPanel();
		panel.add(saveButton);
		panel.add(closeButton);
		return panel;
	}

}
